﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ICIA.ARojas.Silverlight.Entities;
using Didsoft.Silverlight.ViewModel.Data;
using ICIA.ARojas.Silverlight.DataService;
using System.Collections.ObjectModel;
using Didsoft.Silverlight.ViewModel.Commanding.Contracts;
using Didsoft.Silverlight.ViewModel;
using ICIA.ARojas.Silverlight.WCFClient.DestinoCliente;
using ICIA.ARojas.Silverlight.WCFClient.CargoCliente;

namespace ICIA.ARojas.Visual.Destino.ViewModel
{
    public class VmAsignarDestinoModal : DataModelBase<DestinoFisico>
    {
        #region servicios

        DestinoNegociosServicioClient servicioDestino = new DestinoNegociosServicioClient();
        CargoNegociosServicioClient servicioCargo = new CargoNegociosServicioClient();
        
        #endregion

        public event EventHandler onGuardado;

        #region bindables

        private int gestion;
        public int Gestion
        {
            get { return gestion; }
            set { gestion = value; FirePropertyChanged("Gestion"); }
        }


        private ObservableCollection<Cargo> cargos;
        public ObservableCollection<Cargo> Cargos
        {
            get { return cargos; }
            set { cargos = value; FirePropertyChanged("Cargos"); }
        }

        private Cargo cargo;
        public Cargo Cargo
        {
            get { return cargo; }
            set { cargo = value; FirePropertyChanged("Cargo"); }
        }


        private ICIA.ARojas.Silverlight.Entities.Destino destinoEditable;
        public ICIA.ARojas.Silverlight.Entities.Destino DestinoEditable
        {
            get { return destinoEditable; }
            set { destinoEditable = value; FirePropertyChanged("DestinoEditable"); }
        }
        #endregion


        public VmAsignarDestinoModal()
        {
            CmdAsignar = new DelegateCommand<object>(CmdAsignar_Execute, CmdAsignar_CanExecute);
            SetDataService(new DestinoFisicoDataService());
            GetAll();

            Gestion = DateTime.Now.Year+1;

            servicioCargo.ObtenerTodasCompleted += new EventHandler<Silverlight.WCFClient.CargoCliente.ObtenerTodasCompletedEventArgs>(servicioCargo_ObtenerTodasCompleted);
            servicioCargo.ObtenerTodasAsync();

        }

        void servicioCargo_ObtenerTodasCompleted(object sender, Silverlight.WCFClient.CargoCliente.ObtenerTodasCompletedEventArgs e)
        {
            Cargos = e.Result;
        }

        public Persona personaDestino { get; set; }

        #region comandos

        public ICommandEvent CmdAsignar { get; set; }
        void CmdAsignar_Execute(object param)
        {
            ICIA.ARojas.Silverlight.Entities.Destino destino =  DestinoEditable!=null?DestinoEditable:new Silverlight.Entities.Destino();
            destino.DestinoFisico = this.SelectedItem;
            destino.FechaRegistro = DateTime.Now;
            destino.Persona = personaDestino;
            destino.RegistradoPor = ICIA.ARojas.Silverlight.Session.Datos.Current.Usuario.NombreUsuario;
            destino.TieneObservaciones = false;
            destino.Gestion = Gestion;
            destino.Cargo = Cargo;


            servicioDestino.GuardarCompleted +=  servicioDestino_GuardarCompleted;
            servicioDestino.GuardarAsync(destino);
        }

        void servicioDestino_GuardarCompleted(object sender, Silverlight.WCFClient.DestinoCliente.GuardarCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                onGuardado(e.Result,EventArgs.Empty);
            }
            else
            {
                Didsoft.Silverlight.ContentManager.ExceptionMessage.Show(e.Error.Message);
            }


        }
        bool CmdAsignar_CanExecute(object param)
        {

            try
            {
                return ((MyTreeViewItem)param) != null;
            }
            catch (Exception ex)
            {
                return false;
            }
            
        }



        #endregion


    }

   

}
